<template>
  <div>
    <el-button type="text" @click="dialogFormVisible = true" class="left"
      v-hasPermi="'user:save'"
      >添加</el-button
    >

    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="userId" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="180">
      </el-table-column>
      <el-table-column prop="photo" label="照片" width="180"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="del(scope.row)" v-hasPermi="'user:delete'">删除</el-button>
          <el-button @click="updateUI(scope.row)" v-hasPermi="'user:update'">修改</el-button>
          <el-button @click="distributeRoleUI(scope.row)">分配角色</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { deleteUser, getList, saveOrUpdate } from "@/api/user/user.js";

export default {
  mounted() {
    this.getList();
  },
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "180px",
    };
  },
  methods: {
    distributeRoleUI(row) {
      this.$router.push(`/System/DistributeRole/${row.userId}`);
    },
    del(row) {
      this.$confirm("确认删除?")
        .then(() => {
          let userId = row.userId;
          deleteUser(userId).then((r) => {
            if (r.code == 200) {
              this.$message.success("删除用户成功");
              this.getList();
              return;
            }
            this.$message.error("删除用户失败:" + r.msg);
          });
        })
        .catch(() => {});
    },
    updateUI(row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    getList() {
      getList().then((r) => {
        if (r.code == 200) {
          this.tableData = r.data;
        }
      });
    },
    submitForm() {
      let path = !this.form.userId ? "save" : "update";
      saveOrUpdate(path, this.form).then((r) => {
        if (r.code == 200) {
          this.$message.success(r.msg);
          this.dialogFormVisible = false;
          this.form = {};
          this.getList();
          return;
        }
        this.$message.error(r.msg);
      });
    },
  },
};
</script>

<style>
</style>